package com.lovo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    public Queue  orderQueue(){
           //队列去绑定死信交换机
        //关联死信交换机
//        Map<String, Object> args = new HashMap<>(3);
//        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", "deadDirectExchange");
//        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key","deadKey");
//        // x-message-ttl  声明队列的TTL
//        args.put("x-message-ttl",8000);
//        return QueueBuilder.durable("orderQueue").withArguments(args).build();
        return new Queue("orderQueue");
    }
//    //死信队列
//    @Bean
//    public Queue deadQueue(){
//        return new Queue("deadQueue");
//    }
    @Bean
    public DirectExchange deadDirectExchange(){
        return new DirectExchange("deadDirectExchange");
    }

    @Bean
    public DirectExchange orderDirectExchange(){
        return new DirectExchange("orderDirectExchange");
    }
    @Bean
    public Binding orderQueueToOrderDirectExchange(Queue  orderQueue,DirectExchange orderDirectExchange){
        return BindingBuilder.bind(orderQueue).to(orderDirectExchange).with("order");
    }
//    @Bean
//    public Binding deadQueueToDeadDirectExchange(Queue deadQueue,DirectExchange deadDirectExchange){
//      return BindingBuilder.bind(deadQueue).to(deadDirectExchange).with("deadKey");
//    }
}
